<?php
namespace backend\controllers;

use Yii;
use yii\base\Controller;
use backend\models\GrpStyle;
use backend\models\GrpClass;

class EditorController extends Controller
{
    public function actionStyle()
    {
        $model = GrpStyle::find()->where(['father'=>'0'])->all();
        return $this->render('style',[
            'model' => $model,
        ]);
    }
    public function actionClass()
    {
        $model = GrpClass::find()->where(['father'=>'0'])->all();
        return $this->render('class',[
            'model' => $model,
        ]);
    }
    
    public function actionAdd()
    {
        $basePath = Yii::$app->request->referrer;
        $str = substr($basePath,-1);
        if ($str == 'e') {
            $db = 'grp_style';
        }else if ($str == 's') {
            $db = 'grp_class';
        }
        if (Yii::$app->request->isPost) {
            $getName = Yii::$app->request->post('name');
            $father = Yii::$app->request->post('father');
            $type = Yii::$app->request->post('type');
            if ($type == 1) {
                $father =0;
            }
            $sqlArr = [
                1=>'SELECT MAX(ROUND(id/100000000)%100)+1 AS maxId FROM '.$db.' WHERE father = 0',
                2=>'SELECT MAX(ROUND(id/1000000)%100)+1 AS maxId FROM '.$db.' WHERE ROUND(id/100000000) = '.$father,
                3=>'SELECT MAX(ROUND(id/10000)%100)+1 AS maxId FROM '.$db.' WHERE ROUND(id/1000000) = '.$father,
                4=>'SELECT MAX(ROUND(id/100)%100)+1 AS maxId FROM '.$db.' WHERE ROUND(id/10000) = '.$father,
                5=>'SELECT ROUND(MAX(id))%100+1 AS maxId FROM '.$db.' WHERE ROUND(id/100) = '.$father
            ];
            $sql = $sqlArr[$type];
            $makeId = Yii::$app->db->createCommand($sql)->queryOne();
            if ($str == 'e') {
                $addone = new GrpStyle();
            }else if ($str == 's') {
                $addone = new GrpClass();
            }
            $timeArr = [
                0=>0,
                1=>100000000,
                2=>1000000,
                3=>10000,
                4=>100,
                5=>1
            ];
            $newId = $father * $timeArr[$type-1] + intval($makeId['maxId']) * $timeArr[$type];
            $addone->id = $newId;
            $addone->name = $getName;
            $addone->father = $father * $timeArr[$type-1];
            if($addone->save()){
                $res = ['flag'=>1,'id' => $newId];
                echo json_encode($res);
            }else {
                echo 2;
            }
           
        }
    }
    
    public function actionDelete()
    {
        $basePath = Yii::$app->request->referrer;
        $str = substr($basePath,-1);
        if (Yii::$app->request->isPost){
            $type = Yii::$app->request->post('type');
            $getId = Yii::$app->request->post('id');
            $typeArr = [
                1=>99999999,
                2=>999999,
                3=>9999,
                4=>99,
                5=>0
            ];
            $theMax = $getId + $typeArr[$type];
            if ($str == 'e') {
                GrpStyle::deleteAll(['between', 'id', $getId, $theMax]);
            }else if ($str == 's') {
                GrpClass::deleteAll(['between', 'id', $getId, $theMax]);
            }
            echo 1;
        }
    }
    
    public function actionModify()
    {
        $basePath = Yii::$app->request->referrer;
        $str = substr($basePath,-1);
        if (Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            if ($str == 'e') {
                $modifyOne = GrpStyle::find()->where(['id'=>$data['id']])->one();
            }else if ($str == 's') {
                $modifyOne = GrpClass::find()->where(['id'=>$data['id']])->one();
            }
            $modifyOne->name = $data['name'];
            if($modifyOne->save()){
                echo 1;
            }
        }
    }
    
    public function actionGetChild()
    {
        $basePath = Yii::$app->request->referrer;
        $str = substr($basePath,-1);
        if (Yii::$app->request->isPost){
            $getId = Yii::$app->request->post('id');
            $type = Yii::$app->request->post('type');
            $timeArr = [
                0=>0,
                1=>100000000,
                2=>1000000,
                3=>10000,
                4=>100,
                5=>1
            ];
            if ($str == 'e') {
                $model = GrpStyle::find()->where(['father'=>$getId])->asArray()->all();
            }else if ($str == 's') {
                $model = GrpClass::find()->where(['father'=>$getId])->asArray()->all();
            }
            echo json_encode($model,true);
        }
    }
}

